<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>计数器</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      html,
      body {
        height: 100%;
      }

      body {
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #f1f5f8;
      }

      .container {
        display: flex;
        flex-direction: column;
        align-items: center;
        color: #102a42;
      }

      .container h2 {
        font-size: 40px;
        border-radius: 4px;
        margin-bottom: 20px;
      }

      .container .value {
        font-size: 60px;
        font-weight: bolder;
        margin-bottom: 20px;
      }

      .container .button-container {
        display: flex;
        gap: 20px;
      }

      .container .btn {
        text-transform: uppercase;
        background: transparent;
        color: #222;
        letter-spacing: 2px;
        font-weight: 700;
        transition: all 0.3s linear;
        border: 2px solid #222;
        cursor: pointer;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
        border-radius: 4px;
        font-size: 20px;
        padding: 8px 6px;
      }

      .container .btn:hover {
        color: #fff;
        background: #222;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h2>计数器</h2>
      <span class="value">0</span>
      <div class="button-container">
        <button class="btn decrease">减 1</button>
        <button class="btn reset">重置</button>
        <button class="btn increase">增 1</button>
      </div>
    </div>
    <script>
      const $value = document.querySelector(".value");
      const $decrease = document.querySelector(".decrease");
      const $reset = document.querySelector(".reset");
      const $increase = document.querySelector(".increase");

      let num = 0;

      function render() {
        $value.textContent = `${num}`;
      }

      $decrease.addEventListener("click", function (e) {
        num--;
        if (num < 0) {
          $value.style.color = "red";
        }
        render();
      });
      $reset.addEventListener("click", function (e) {
        num = 0;
        $value.style.color = "";
        render();
      });
      $increase.addEventListener("click", function (e) {
        num++;
        if (num > 0) {
          $value.style.color = "green";
        }
        render();
      });
    </script>
  </body>
</html>
